import 'package:flutter/material.dart';

/// 不同类型条目的列表
/// 1. 创建一个不同类型条目的数据源
/// 2. 转换数据源为不同的控件

class DifferentTypesApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final String appTitle = '';
    return MaterialApp(
      title: appTitle,
      theme: ThemeData(primarySwatch: Colors.red),
      home: DifferentTypesAppPage(title: appTitle),
    );
  }
}

class DifferentTypesAppPage extends StatefulWidget {
  final String title;

  DifferentTypesAppPage({Key key, this.title}) : super(key: key);

  @override
  State<StatefulWidget> createState() => DifferentTypesAppPageState();
}

class DifferentTypesAppPageState extends State<DifferentTypesAppPage> {
  // 创建列表数据
  final items = List<ListItem>.generate(
      1200,
      (i) => i % 6 == 0
          ? HeadingItem("Heading $i")
          : MessageItem("Sender $i", "Message body $i"));
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: ListView.builder(
          itemCount: items.length,
          itemBuilder: (context, index) {
            final item = items[index];
            if (item is HeadingItem) {
              return ListTile(
                title: Text(
                  item.heading,
                  style: Theme.of(context).textTheme.headline,
                ),
              );
            } else if (item is MessageItem) {
              return ListTile(
                title: Text(item.sender),
                subtitle: Text(item.body),
              );
            }
          }),
    );
  }
}

/// 列表条目
abstract class ListItem {}

/// 头条目
class HeadingItem implements ListItem {
  final String heading;
  HeadingItem(this.heading);
}

/// 消息条目
class MessageItem implements ListItem {
  final String sender;
  final String body;
  MessageItem(this.sender, this.body);
}
